package tomenttosa.prunuus.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import com.google.common.collect.MapMaker;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreeMarkerUtils {

	private static ConcurrentMap<String, Configuration> templateConfigMap = new MapMaker().makeMap();
	
	public static Configuration stringConfig = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
	
	static {
		stringConfig.setTemplateLoader(new StringTemplateLoader());
	}

	public static Configuration getConfiguration(String directory) throws IOException {

		Configuration configuration = templateConfigMap.get(directory);

		if (configuration == null) {
			File dir = new File(directory);
			configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
			configuration.setDefaultEncoding("utf-8");
			configuration.setDirectoryForTemplateLoading(dir);
			templateConfigMap.put(directory, configuration);
		}

		return configuration;
	}
	
	public static void createFromTemplate(String templateFileName, Map<String, Object> params, String outputFileName) throws Exception {
		Writer out = null;
		try {
			File templateFile = new File(templateFileName);

			Template template = getConfiguration(templateFile.getParent()).getTemplate(templateFile.getName());

			File docFile = new File(outputFileName);
			File parentFile = docFile.getParentFile();
			if (!parentFile.exists()) {
				parentFile.mkdirs();
			}

			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFileName), "utf-8"));
			template.process(params, out);
		} catch (Exception e) {
			throw e;
		} finally {
			if (out != null) {
				out.close();
			}
		}

	}

	public static String createFromTemplate(String templateFileName, Map<String, Object> params) throws Exception {
		File templateFile = new File(templateFileName);
		return createFromTemplate(templateFile, params);
	}

	public static String createFromTemplate(File templateFile, Map<String, Object> params) throws Exception {
		Writer out = null;
		String str = null;
		try {

			Template template = getConfiguration(templateFile.getParent()).getTemplate(templateFile
					.getName());

			out = new StringWriter();
			template.process(params, out);

			out.flush();
			str = out.toString();

		} catch (Exception e) {
			throw e;
		} finally {
			if (out != null) {
				out.close();
			}
		}

		return str;
	}
	
	public static String createFromString(Map<String, Object> params, String tempId, String templateString) throws Exception {

		Writer out = null;
		String str = null;
		try {
			
			Configuration cfg = stringConfig;
			((StringTemplateLoader)cfg.getTemplateLoader()).putTemplate(tempId, templateString);

			Template temp = cfg.getTemplate(tempId, "utf-8");
			out = new StringWriter(2048);
			temp.process(params, out);

			out.flush();
			str = out.toString();
		} catch (Exception e) {
			throw e;
		} finally {
			if (out != null) {
				out.close();
			}
		}
		return str;
	}
	
	
}
